app.controller('knowledgeDataController', ['$scope', "$rootScope",'$state', '$stateParams', '$http', 'ControllerConfig','$translate','$compile',
    function ($scope,$rootScope, $state, $stateParams, $http, ControllerConfig,$translate,$compile) {

        $scope.$on('$viewContentLoaded', function (event) {
            $http({
                method : 'GET',
                url : "/Portal/Organization/GetCurrentUser",
            }).success(function(result) {
                //debugger;
                $scope.currentUser = result.User;
                $scope.departName = result.OUDepartName;
            })
            var paramList = window.location.href.split('=');
            if(paramList.length = 2 ) {
                $scope.searchSql = paramList[1];
            } else {
                $scope.searchSql = "";
            }
            $scope.init();
        });


    $scope.pageSize = 10;

    $scope.pageIndex = 1;

    $scope.maxshowpageitem = 10;

    /**
     * 初始化数据处理
     * @param listCount 列表总量
     * @param currentPage 当前页
     */
    $scope.initPage = function(listCount,currentPage){
        var maxshowpageitem = $scope.maxshowpageitem;
        if(maxshowpageitem!=null&&maxshowpageitem>0&&maxshowpageitem!=""){
            $scope.maxshowpageitem = maxshowpageitem;
        }
        var pagelistcount = $scope.pageSize;
        if(pagelistcount!=null&&pagelistcount>0&&pagelistcount!=""){
            $scope.pageSize = pagelistcount;
        }
        $scope.pageSize =pagelistcount;
        if(listCount<0){
            listCount = 0;
        }
        if(currentPage<=0){
            currentPage=1;
        }

        $scope.initWithUl(listCount,currentPage);
    },
        /**
         * 初始化分页界面
         * @param listCount 列表总量
         */
        $scope.initWithUl = function(listCount,currentPage){
            var pageCount = 1;
            if(listCount>=0){
                var pageCount = listCount % $scope.pageSize>0?parseInt(listCount/$scope.pageSize)+1:parseInt(listCount/$scope.pageSize);
            }
            var appendStr = $scope.getPageListModel(pageCount,currentPage);
            $("#page").html($compile(appendStr)($scope));
        },


        $scope.getPageListModel = function(pageCount,currentPage){
            var prePage = currentPage-1;
            var nextPage = currentPage+1;
            var prePageClass ="pageItem";
            var nextPageClass = "pageItem";
            if(prePage<=0){
                prePageClass="pageItemDisable";
            }
            if(nextPage>pageCount){
                nextPageClass="pageItemDisable";
            }
            var appendStr ="";
            appendStr+="<li><span>每页显示</span><select ng-change='GetDatas(1)'  ng-model= \"pageSize\" ><option value=\"10\" >10</option><option value=\"15\" >15</option>\n" +
                "            <option value=\"50\" >50</option>\n" +
                "            <option value=\"100\" >100</option></select><span>条</span></li>";

            appendStr+="<li class='"+prePageClass+"' page-data='1' page-rel='firstpage' ng-click='GetDatas(1)'>首页</li>";
            appendStr+="<li class='"+prePageClass+"' page-data='"+prePage+"' page-rel='prepage' ng-click='GetDatas("+(parseInt($scope.pageIndex)==1?1:parseInt($scope.pageIndex-1))+")'>&lt;</li>";
            var miniPageNumber = 1;
            if(currentPage-parseInt($scope.pageSize/2)>0&&currentPage+parseInt($scope.pageSize/2)<=pageCount){
                miniPageNumber = currentPage-parseInt($scope.pageSize/2);
            }else if(currentPage-parseInt($scope.pageSize/2)>0&&currentPage+parseInt($scope.pageSize/2)>pageCount){
                miniPageNumber = pageCount-$scope.pageSize+1;
                if(miniPageNumber<=0){
                    miniPageNumber=1;
                }
            }
            var showPageNum = parseInt($scope.pageSize);
            if(pageCount<showPageNum){
                showPageNum = pageCount;
            }
            for(var i=0;i<showPageNum;i++){
                var pageNumber = miniPageNumber++;
                var itemPageClass = "pageItem";
                if(pageNumber==currentPage){
                    itemPageClass = "pageItemActive";
                }

                appendStr+="<li class='"+itemPageClass+"' page-data='"+pageNumber+"' page-rel='itempage' ng-click='GetDatas("+pageNumber+")'>"+pageNumber+"</li>";
            }
            if(showPageNum==0){
                appendStr+="<li class='pageItemActive' page-data='1' page-rel='itempage' ng-click='GetDatas(1)'>"+1+"</li>";
            }
            appendStr+="<li class='"+nextPageClass+"' page-data='"+nextPage+"' page-rel='nextpage' ng-click='GetDatas("+(parseInt($scope.pageIndex)>=parseInt(pageCount)?parseInt(pageCount):parseInt($scope.pageIndex+1))+")'>&gt;</li>";
            appendStr+="<li class='"+nextPageClass+"' page-data='"+pageCount+"' page-rel='lastpage' ng-click='GetDatas("+pageCount+")'>尾页</li>";
            return appendStr;

        }



        $scope.filteringDigital = function (numberData){

             var  numberData = numberData.replace(/[^\d.]/g,'');

        }



        $scope.startUpdate = function (instanceId) {
            var msg = "点击修改会发起修改流程,您真的确定要修改吗？\n\n请确认！";
            if (confirm(msg)==true){
                debugger;
                window.open("/Portal/kumhosunny/knowledgeupdate/updateKnowledgeStart?instanceId="+instanceId,"_blank");
            }else{
                return false;
            }

        }

        $scope.startDelete = function (instanceId) {
            var msg = "点击删除会发起删除流程,您真的确定要删除吗？\n\n请确认！";
            if (confirm(msg)==true){
                debugger;
                window.open("/Portal/kumhosunny/knowledge/createKnowledgeDeleteProcess?knowledgeInstanceId="+instanceId,"_blank");
            }else{
                return false;
            }

        }


    $scope.init = function() {
        $scope.GetDatas(1);
        $scope.getHy();
    };

        $scope.doSearch = function () {
            $scope.searchSql = "";
            $scope.GetDatas(1);
        }


    $scope.GetDatas = function (pageIndex) {
        $scope.pageIndex = pageIndex;
        var targetUrl = "/Portal/kumhosunny/knowledge/searchData";
        var sqlStr = localStorage.getItem('sqlStr');
        if ($scope.searchSql && sqlStr) {
            targetUrl = "/Portal/kumhosunny/knowledge/searchDataBySenior";
        }
        $http({
            method:'GET',
            url:targetUrl,
            params : {
                "pageIndex" : $scope.pageIndex,
                "pageSize" : $scope.pageSize,
                "pageStart" : $scope.pageSize * ($scope.pageIndex - 1),
                "searchKey" : $scope.searchKey,
                "industry" : $scope.industry,
                "subIndustry" : $scope.subIndustry,
                "parts" : $scope.parts,
                "searchSql" : sqlStr
            }
        }).success(function(result){
            if(result.Success) {
                $scope.datas = result.Extend;
                $scope.dataCount = result.LoadData;
                $scope.initPage($scope.dataCount,$scope.pageIndex);
                console.log($scope.datas);
            } else {

            }
        });
    }
        $scope.industryList=[];
        $scope.subIndustryList=[];
        $scope.partsList=[];
        $scope.getHy=function(){

          $.ajax({
              type:"POST",
              async :false,
              url:"/Portal/kumhosunny/knowledge/getIndustry",
              success:function(data){
                  $scope.industryList=data.data ;
              }
          })


        }

        $scope.changezHy=function(){
            $.ajax({
                type:"POST",
                async :false,
                data:{
                    type:"1",
                    value: $scope.industry
                },
                url:"/Portal/kumhosunny/knowledge/getIndustry",
                success:function(data){
                      $scope.subIndustryList=data.data ;
                }
            })


        }
        $scope.changelbj=function(){
            $.ajax({
                type:"POST",
                async :false,
                data:{
                    type:"2",
                    value: $scope.subIndustry
                },
                url:"/Portal/kumhosunny/knowledge/getIndustry",
                success:function(data){
                     $scope.partsList=data.data ;
                }
            })

        }



        //$scope.init();

    }]);